package com.aos.erp.schools.service.student.imp;

import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.schools.cutils.Constant;
import com.aos.erp.schools.enums.student.IntegerStatus;
import com.aos.erp.schools.mapper.student.StudentMapper;
import com.aos.erp.schools.model.student.Student;
import com.aos.erp.schools.model.student.dto.GovExNewStudentDTO;
import com.aos.erp.schools.model.student.dto.StudentOneDto;
import com.aos.erp.schools.model.student.ro.news.NewsRo;
import com.aos.erp.schools.model.student.ro.student.*;
import com.aos.erp.schools.model.student.vo.news.NewsVo;
import com.aos.erp.schools.model.student.vo.student.StudentAll2Vo;
import com.aos.erp.schools.model.student.vo.student.StudentAllVo;
import com.aos.erp.schools.service.gatherschool.GatherSchoolService;
import com.aos.erp.schools.service.student.StudentService;
import com.aos.erp.schools.service.mq.StudentSender;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 学员信息表 服务实现类
 * </p>
 *
 * @author chb
 * @since 2018-10-31
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Resource
    private StudentMapper  studentMapper;
    @Resource
    private GatherSchoolService schoolService;
    @Resource
    private StudentSender studentSenderMq;

    /**
     *
     * @param studentRo
     * @throws Exception  学员添加成功
     */
    @Override
    public void saveStudent(AddStudentRo studentRo) throws Exception {

        QueryWrapper qw=new QueryWrapper();
        //根据身份证号码  查询学员是否重复
        qw.eq("certificates_number",studentRo.getCertificatesNumber());
        Student oldStudent=this.getOne(qw);
        if(oldStudent!=null)
        {
            throw new BizException("学员不能重复添加!");
        }
        Student student=new Student();
        BeanUtils.copyProperties(studentRo,student);
        student.setCreated(new Date());
        student.setUpdated(new Date());
        //1.总校 操作
        if(studentRo.getDriversSchoolType()== Constant.schoolTypeOne)
        {
            //总校 -->   提交按钮  ----->互联网状态变为  待提交
            if(studentRo.getMyState()==Constant.zero)//立即提交按钮
            {
                student.setNetStatus(IntegerStatus.waitsubmit);
            }
            //总校 -- 保存  -- 互联网状态变为  未提交
            if(studentRo.getMyState()==Constant.one)//保存
            {
                student.setNetStatus(IntegerStatus.noschool);
            }
        }
        //2. 非总校操作
        if(studentRo.getDriversSchoolType()!= Constant.schoolTypeOne)
        {
            //非总校 -- 提交按钮 --直接提交 总校 ----->互联网状态变为  提交至总校
            if(studentRo.getMyState()==Constant.zero)//立即提交按钮
            {
                student.setNetStatus(IntegerStatus.yesschool);
            }
            //非总校总校 -- 保存  -- 互联网状态变为  未提交总校
            if(studentRo.getMyState()==Constant.one)//保存
            {
                student.setNetStatus(IntegerStatus.noschool);
            }
        }

        //TODO状态需要完善的后期实体
        this.save(student);
    }

    /**
     *
     * @param
     * @return
     * 学员修改
     */
    @Override
    public int updateStudent(UpdateStudentRo studentRo) {

        Student student=new Student();
        BeanUtils.copyProperties(studentRo,student);
        //1.总校 操作
        if(studentRo.getDriversSchoolType()== Constant.schoolTypeOne)
        {
            //总校 -->   提交按钮  ----->互联网状态变为  待提交
            if(studentRo.getMyState()==Constant.zero)//立即提交按钮
            {
                student.setNetStatus(IntegerStatus.waitsubmit);
            }
            //总校 -- 保存  -- 互联网状态变为  未提交
            if(studentRo.getMyState()==Constant.one)//保存
            {
                student.setNetStatus(IntegerStatus.noschool);
            }
        }
        //2. 非总校操作
        if(studentRo.getDriversSchoolType()!= Constant.schoolTypeOne)
        {
            //非总校 -- 提交按钮 --直接提交 总校 ----->互联网状态变为  提交至总校
            if(studentRo.getMyState()==Constant.zero)//立即提交按钮
            {
                student.setNetStatus(IntegerStatus.yesschool);
            }
            //非总校总校 -- 保存  -- 互联网状态变为  未提交总校
            if(studentRo.getMyState()==Constant.one)//保存
            {
                student.setNetStatus(IntegerStatus.noschool);
            }
        }
        student.setUpdated(new Date());
        return studentMapper.updateStudent(student);
    }

    /**
     * 分页查询学员
     * @param ro
     * @return
     */
    @Override
    public PageBean<StudentAllVo> queryByPage(SelectStudentRo ro) {

        Page<StudentAllVo> page = new Page<StudentAllVo>(ro.getPageIndex(), ro.getPageSize());
        List<StudentAllVo> noticeInfoList = baseMapper.queryList(page, ro);
        page.setRecords(noticeInfoList);

        PageBean<StudentAllVo> bean = new PageBean<StudentAllVo>(page.getCurrent(), page.getSize(), page.getTotal());
        bean.setOptimizeCountSql(page.optimizeCountSql());
        bean.setAscs(page.ascs());
        bean.setDescs(page.descs());
        bean.setRecords(page.getRecords());
        bean.condition();
        return bean;
    }



    @Override
    public int deleteStudent(Long[] ids) {
        return studentMapper.deleteStudent(ids);
    }

    @Override
    public Student selectOne(StudentOneDto dto) {

        Condition condition = new Condition(Student.class);
        if (StringUtils.isNotBlank(dto.getName())) {
            condition.eqFiled("name", dto.getName());
        }
        if (dto.getDriverId()!=0) {
            condition.eqFiled("driversId", dto.getDriverId());
        }
        if (dto.getTraining()!=null) {
            condition.eqFiled("status", dto.getTraining());
        }
        return studentMapper.selectOne(condition.getWrapper());
    }

    @Override
    public PageBean<StudentAllVo> queryListByCityId(ListByCityIdRpcRo ro) {
        Page<StudentAllVo> page = new Page<StudentAllVo>(ro.getPageIndex(), ro.getPageSize());
        List<StudentAllVo> noticeInfoList = baseMapper.queryListByCityId(page, ro);
        page.setRecords(noticeInfoList);

        PageBean<StudentAllVo> bean = new PageBean<StudentAllVo>(page.getCurrent(), page.getSize(), page.getTotal());
        bean.setOptimizeCountSql(page.optimizeCountSql());
        bean.setAscs(page.ascs());
        bean.setDescs(page.descs());
        bean.setRecords(page.getRecords());
        bean.condition();

        return bean;
    }

    @Override
    public int getStateStudentNum(long driverId) {
        return studentMapper.getStateStudentNum(driverId);
    }

    @Override
    public PageBean<StudentAllVo> queryListBy122(SelectSubmit122Ro ro) {
        Page<StudentAllVo> page = new Page<StudentAllVo>(ro.getPageIndex(), ro.getPageSize());
        List<StudentAllVo> noticeInfoList = baseMapper.queryListBy122(page, ro);
        page.setRecords(noticeInfoList);

        PageBean<StudentAllVo> bean = new PageBean<StudentAllVo>(page.getCurrent(), page.getSize(), page.getTotal());
        bean.setOptimizeCountSql(page.optimizeCountSql());
        bean.setAscs(page.ascs());
        bean.setDescs(page.descs());
        bean.setRecords(page.getRecords());
        bean.condition();
        return  bean;
    }

    @Override
    public int updateNetStatus(Long[] ids) {

        //发送 MQ 给122 平台
        //查询所有的学生
         List<GovExNewStudentDTO>   listVo=studentMapper.queryObjectList(ids);

        try{
            studentSenderMq.sendStudent(listVo);
            return    studentMapper.updateNetStatus(ids);
        }catch(Exception e){

            return 0;
        }

    }

    @Override
    public int updateNetStatusFaild(List<String> idnumList) {
        return studentMapper.updateNetStatusFaild(idnumList);
    }

    @Override
    public int signUpOkStatus(List<String> idnumList) {
        return studentMapper.signUpOkStatus(idnumList);
    }

    @Override
    public int signUpFaildStatus(String idnum) {
        return studentMapper.signUpFaildStatus(idnum);
    }

    @Override
    public List<GovExNewStudentDTO> queryObjectList(Long[] ids) {
        return studentMapper.queryObjectList(ids);
    }

    @Override
    public StudentAll2Vo selectAll2Vo(long id) {
        return studentMapper.selectAll2Vo(id);
    }


    @Override
    public int updateNetStatusSuccess(List<String> idnumList) {
        return studentMapper.updateNetStatusSuccess(idnumList);
    }

    @Override
    public PageBean<NewsVo> selectStudentByIdNews(NewsRo ro) {
        Page<NewsVo> page = new Page<NewsVo>(ro.getPageIndex(), ro.getPageSize());
        List<NewsVo> noticeInfoList = baseMapper.selectStudentByIdNews(page, ro);
        page.setRecords(noticeInfoList);

        PageBean<NewsVo> bean = new PageBean<NewsVo>(page.getCurrent(), page.getSize(), page.getTotal());
        bean.setOptimizeCountSql(page.optimizeCountSql());
        bean.setAscs(page.ascs());
        bean.setDescs(page.descs());
        bean.setRecords(page.getRecords());
        bean.condition();
        return bean;
    }

    @Override
    public List<StudentAllVo> excelStudentExport(Long[] ids) {
        return baseMapper.excelStudentExport(ids);
    }

    @Override
    public int updateStudentByStatus(Long studentId,int status) {
        return baseMapper.updateStudentByStatus(studentId,status);
    }

    @Override
    public StudentAllVo queryObject(long id) {
        return studentMapper.queryObject(id);
    }
}
